import java.io.*;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 18/4/16.
 */
public class IOUtis {


    public static void saveUser(User user) {



        Statement statement = JDBCUtil.getStatement();
        String  sql = "select * from student";

        try {
            ResultSet resultSet = statement.executeQuery(sql);
            while (resultSet.next()){

                String Uname =  resultSet.getString("username");
                String Ppwd  =  resultSet.getString("pwd");

                if (!Uname.equals(user.getUsername())){
                     String sqlInfo ="insert into code_jdbc.student(username,pwd,word) " + "values('" + user.getUsername() + "','" + user.getPwd() + "','" + "HAHAHAHAHHA" + "')";
                     Statement statement1 = JDBCUtil.getStatement();
                     int rows = statement1.executeUpdate(sqlInfo);

                }else {
                    System.out.println("用户名已存在");
                }


            }

        } catch (Exception e) {
            e.printStackTrace();
        }


        JDBCUtil.closeResource(statement);
        JDBCUtil.closeConn();
    }


    // 取用户
    public static ArrayList<User> initUsers(){


        ArrayList<User> users = new ArrayList<>();
        Statement statement = JDBCUtil.getStatement();
        String  sql = "select * from student";

        try {
            ResultSet resultSet = statement.executeQuery(sql);
            while (resultSet.next()){
                String Uname =  resultSet.getString("username");
                String Ppwd  =  resultSet.getString("pwd");
                String motto=  resultSet.getString("word");
                User user = new User();
                    user.setUsername(Uname);
                    user.setPwd(Ppwd);
                    user.setMotto(motto);
                    users.add(user);

            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        JDBCUtil.closeResource(statement);
        JDBCUtil.closeConn();
        return users;

    }



    //判断 是否存在已有账户
    public static Boolean noSameusername(List<User> users,String username){
        Boolean isSame = false;
        if (users == null){
            return null;
        }
        for (User user : users) {
            if (username.equals(user.getUsername())){
                isSame = true;
                break;
            }
        }
        return isSame;
    }

    // 封装关闭流的方法 ,有几个流需要关,就传几个参数,提高代码的整洁度
    public static void closeAll(Closeable... closeables) {  //Closeable 是有close方法的实现类

        if (closeables == null) {  //判断数组是否为空
            return;
        }

        for (Closeable closeable : closeables) {
            if (closeable != null) {
                try {
                    closeable.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }

}
